package com.leyou.cart.mq;

import com.leyou.cart.service.CartService;
import com.leyou.common.constants.MQConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class CartListener {

    @Autowired
    private CartService cartService;

    /**
     * 根据skuId批量删除购物车
     * @param messageMap
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value= MQConstants.Queue.CART_DELETE_KEY_QUEUE,durable = "true"), // 定义队列的名称和是否持久化
                    exchange = @Exchange(
                            value = MQConstants.Exchange.CART_EXCHANGE_NAME,
                            type = ExchangeTypes.TOPIC,
                            // springboot会自动创建交换机：如果为true：创建交换机失败之后继续创建其他属性
                            ignoreDeclarationExceptions = "true"
                    ),
                    key = MQConstants.RoutingKey.CART_DELETE_KEY
            )
    )
    public void deleteCartBySkuIds(Map<String, List<Long>> messageMap){
        log.info("【购物车服务】根据skuid购物车");
        cartService.deleteCartBySkuIds(messageMap);
    }
}
